package com.easyaop.core.matcher;

import org.objectweb.asm.Type;

import com.easyaop.api.pointcut.MethodObjectForBuddy;
import com.easyaop.api.pointcut.MethodPointcut;
import com.easyaop.api.pointcut.PointcutParser;
import com.easyaop.commons.MethodSignature;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class CustomMethodMatcher implements ElementMatcher<MethodDescription> {
    private final PointcutParser pointcutParser;

    private final MethodPointcut methodPointcut;

    public CustomMethodMatcher(PointcutParser pointcutParser, MethodPointcut methodPointcut) {
        this.pointcutParser = pointcutParser;
        this.methodPointcut = methodPointcut;
    }

    @Override
    public boolean matches(MethodDescription methodDescription) {
        if (true) {
            return methodPointcut.isMethod(new MethodObjectForBuddy(methodDescription));
        }

        // 带1后缀的是入参相关的变量，不带1后缀的this相关的变量。
        String methodName1 = methodDescription.getName();
        String descriptor1 = methodDescription.getDescriptor();

        if (!pointcutParser.isMethodName(methodName1)) {
            return false;
        }

        MethodSignature methodSignature = methodPointcut.getMethodSignature();
        if (methodSignature.getDescriptor().startsWith("(..)")) {
            // 匹配任意入参
            return true;
        }

        Type type1 = Type.getType(descriptor1);
        Type[] argumentTypes1 = type1.getArgumentTypes();
        Type[] argumentTypes = methodSignature.getArgumentTypes();

        if (argumentTypes1.length != argumentTypes.length) {
            return false;
        }
        if (descriptor1.equals(methodSignature.getDescriptor())) {
            return true;
        }
        return false;
        //        }
        //        for (int i = 0; i < argumentTypes1.length; i++) {
        //            String className = argumentTypes1[i].getClassName();
        //            String voParamType = paramTypes[i];
        //            if (className.equals(voParamType)) {
        //                continue;
        //            }
        //            if (className.startsWith("java.lang") || className.startsWith("java.util")) {
        //                String simpleClassName = className.substring(className.lastIndexOf(".") + 1);
        //                if (simpleClassName.equals(voParamType)) {
        //                    continue;
        //                }
        //            }
        //            return false;

    }
/*@Override
    public boolean matches(MethodDescription methodDescription) {
        // 自定义匹配逻辑，例如基于参数类型或其他条件
        String methodName = methodDescription.getName();
        String descriptor = methodDescription.getDescriptor();

        Type type = Type.getType(descriptor);
        Type[] argumentTypes = type.getArgumentTypes();
        if (!pointcutParser.isMethodName(methodName)) {
            return false;
        }
        String[] paramTypes = methodPointcut.getParamTypes();
        if (paramTypes != null && paramTypes.length == 1 && "..".equals(paramTypes[0])) {
            // 匹配任意入参
            return true;
        }
        if (argumentTypes.length != paramTypes.length) {
            return false;
        }
        for (int i = 0; i < argumentTypes.length; i++) {
            String className = argumentTypes[i].getClassName();
            String voParamType = paramTypes[i];
            if (className.equals(voParamType)) {
                continue;
            }
            if (className.startsWith("java.lang") || className.startsWith("java.util")) {
                String simpleClassName = className.substring(className.lastIndexOf(".") + 1);
                if (simpleClassName.equals(voParamType)) {
                    continue;
                }
            }
            return false;
        }
        return true;
    }*/
}